package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.content.ConBookMapper;
import org.qs.webservice.dao.user.UsBookReviewMapper;
import org.qs.webservice.dao.user.UsBookShelfMapper;
import org.qs.webservice.dao.user.UsSubscribeAutoMapper;
import org.qs.webservice.dao.user.UsUserMapper;
import org.qs.webservice.domain.ConBook;
import org.qs.webservice.domain.UsBookReview;
import org.qs.webservice.domain.UsBookReviewReply;
import org.qs.webservice.domain.UsBookShelf;
import org.qs.webservice.domain.UsSubscribeAuto;
import org.qs.webservice.domain.UsUser;
import org.qs.webservice.service.AuthorService;
import org.qs.webservice.service.BookService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.Book;
import org.qs.webservice.vo.BookReview;
import org.qs.webservice.vo.BookReviewReply;
import org.qs.webservice.vo.Content;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;


@Service
public class AuthorServiceImpl implements AuthorService {
	@Resource
	private UsBookShelfMapper usBookShelfMapper;
	@Resource
	private UsBookReviewMapper usBookReviewMapper;
	@Resource
	private UsUserMapper usUserMapper;
	@Resource
	private ConBookMapper conBookMapper;
	@Resource
	private UsSubscribeAutoMapper usSubscribeAutoMapper;
	@Resource
	private BookService bookService;
	@Override
	public String getCollect(String author_id,int pages, int contents, String token,
			String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
			
		Long author_id_Long = Long.valueOf(author_id);
		UsUser usUser = usUserMapper.getCustomUserByUserid(author_id_Long);
		if(null==usUser){
			tokenMap.put("code", 1002);
			tokenMap.put("reason", "用户不存在");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(usUser.getChecked()!=1){
			tokenMap.put("code", 1001);
			tokenMap.put("reason", "该用户不是作者");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		int index=(pages-1)*contents;
		List<UsBookShelf> list = usBookShelfMapper.getShelf(index, contents, Long.valueOf(author_id_Long));
		List<Content>books = new ArrayList<Content>();
		for(UsBookShelf usBookShelf :list){
			Content bookShelf = new Content();
			bookShelf.setMark_id(usBookShelf.getMark_id()+"");
			bookShelf.setContent_id(usBookShelf.getBook_id());
			bookShelf.setContent_name(usBookShelf.getBook_name());
			if(usBookShelf.getChap_id()==0){
				bookShelf.setChapter_id(usBookShelf.getFirst_chap_id());
			}else
				bookShelf.setChapter_id(usBookShelf.getChap_id());
			bookShelf.setChapter_name(usBookShelf.getChap_name());
			bookShelf.setAuthor_name(usBookShelf.getAuthor_name());
			bookShelf.setAuthor_id(usBookShelf.getAuthor_id()+"");
			bookShelf.setChapter_offset(usBookShelf.getChapter_offset());
			bookShelf.setMark_time(DateUtil.getString(usBookShelf.getMark_time(), "yyyy-MM-dd HH:mm:ss"));
			books.add(bookShelf);
		}
		
		PageData<Content> pageData = new PageData<Content>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		pageData.setContent(books);
		pageData.setTotal(usBookShelfMapper.getShelfNum(Long.valueOf(author_id_Long)));
		return IReadUtils.beanToJson(pageData);
	}
	@Override
	public String getReviews(String author_id, int pages, int contents,
			String token, String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		
		Long author_id_Long = Long.valueOf(author_id);
		UsUser usUser = usUserMapper.getCustomUserByUserid(author_id_Long);
		if(null==usUser){
			tokenMap.put("code", 1002);
			tokenMap.put("reason", "用户不存在");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(usUser.getChecked()!=1){
			tokenMap.put("code", 1001);
			tokenMap.put("reason", "该用户不是作者");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		
		PageData<BookReview> pageData = new PageData<BookReview>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		int index=(pages-1)*contents;
		
		pageData.setTotal(usBookReviewMapper.getBookReviewListByUserNum(Long.valueOf(author_id)));
		List<UsBookReview> usBookReviews = usBookReviewMapper.getBookReviewListByUser(Long.valueOf(author_id), index, contents);
		List<BookReview> list = new ArrayList<BookReview>();
		for(UsBookReview usBookReview:usBookReviews){
			BookReview bookReview = new BookReview();
			bookReview.setContent_id(usBookReview.getReview_id()+"");
			bookReview.setContent(usBookReview.getContent());
			bookReview.setBook_id(usBookReview.getBook_id()+"");
			bookReview.setBook_name(usBookReview.getBook_name());
			bookReview.setCreate_time(DateUtil.getString(usBookReview.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
			if(usBookReview.getEssence()==1){
				bookReview.setEssence("[精华]");
			}else{
				bookReview.setEssence("");
			}
			bookReview.setReply_cnt(usBookReview.getReply_cnt());
			bookReview.setTitle(usBookReview.getTitle());
			if(usBookReview.getTop()==1){
				bookReview.setTop("[置顶]");
			}else{
				bookReview.setTop("");
			}
			bookReview.setUser_id(usBookReview.getUser_id()+"");
			bookReview.setUser_name(usBookReview.getUser_name());
			List<BookReviewReply> replys = new ArrayList<BookReviewReply>();
			for(UsBookReviewReply usBookReviewReply:usBookReview.getReplys()){
				BookReviewReply reply = new BookReviewReply();
				if(usBookReviewReply.getReply_id()==0)continue;
				reply.setContent(usBookReviewReply.getReply_id()+"");
				reply.setContent(usBookReviewReply.getContent());
				reply.setCreate_time(DateUtil.getString(usBookReviewReply.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
				reply.setUser_id(usBookReviewReply.getUser_id()+"");
				reply.setUser_name(usBookReviewReply.getUser_name());
				if(usBookReviewReply.getUser_id().equals(usBookReview.getAuthor_id()))
					reply.setIs_author(1);
				replys.add(reply);
			}
			bookReview.setReplys(replys);
			bookReview.setIamge_url(usBookReview.getImage_url());
			if(usBookReview.getUser_id().equals(usBookReview.getAuthor_id()))
				bookReview.setIs_author(1);
			list.add(bookReview);
		}
		pageData.setContent(list);
		return IReadUtils.beanToJson(pageData); 
	}
	@Override
	public String getWorks(String author_id, int pages, int contents,
			String token, String device_identifier) {
Map<String,Object> tokenMap = new HashMap<String,Object>();
		
		Long author_id_Long = Long.valueOf(author_id);
		UsUser usUser = usUserMapper.getCustomUserByUserid(author_id_Long);
		if(null==usUser){
			tokenMap.put("code", 1002);
			tokenMap.put("reason", "用户不存在");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(usUser.getChecked()!=1){
			tokenMap.put("code", 1001);
			tokenMap.put("reason", "该用户不是作者");
			return IReadUtils.beanToJson(tokenMap);
		}
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		
		PageData<Book> pageData = new PageData<Book>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		int index=(pages-1)*contents;
		List<Book> books = new ArrayList<Book>();
		List<ConBook> conBooks = conBookMapper.getBookByUser(Long.valueOf(author_id),index,contents);
		for(ConBook conBook:conBooks){
			Book book = bookService.setBookInfo(conBook);
			String user_id = IReadUtils.getUserIdByTokenWithoutEffective(token);
			if(!StringUtils.isNullOrEmpty(user_id)){
				//获取是否自动订购
				UsSubscribeAuto usSubscribeAuto = usSubscribeAutoMapper.getUsSubscribeById(Long.valueOf(user_id),conBook.getBook_id());
				if(usSubscribeAuto!=null&&usSubscribeAuto.getStatus()==1){
					book.setSubscribe_auto(1);
				}
				if(user_id.equals(book.getAuthor_id())){
					book.setIs_author(1);
				}
				UsBookShelf usBookShelf = usBookShelfMapper.getShelfByBookIdAndUserId(conBook.getBook_id(), Long.valueOf(user_id));
				if(usBookShelf!=null){
					book.setCollect_status(1);
				}
		//		books.add(book)
			}
			books.add(book);
		}
		pageData.setContent(books);
		pageData.setTotal(conBookMapper.getBookNumByUser(author_id_Long));
		return IReadUtils.beanToJson(pageData);
	}

	
}
